﻿#region Using

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace XNgine.ScreenManagement.StandardScreens.Menu
{
    /// <summary>  
    /// Helper class represents a single entry in a MenuScreen. By default this  
    /// just draws the entry text string, but it can be customized to display menu  
    /// entries in different ways. This also provides an event that will be raised  
    /// when the menu entry is Selected or when Activated.
    /// </summary>  
    public class MenuItem
    {
        /// <summary>  
        /// A default font for menu items
        /// </summary>  
        public static SpriteFont Font { get; set; }

        /// <summary>
        /// Caption of the menu item
        /// </summary>
        public string Caption { get; set; }

        /// <summary>  
        /// Tracks a fading selection effect on the entry.  
        /// </summary>  
        /// <remarks>  
        /// The entries transition out of the selection effect when they are deselected.  
        /// </remarks>
        private float selectionFade;

        /// <summary>  
        /// Event raised when the menu entry is activated.  
        /// </summary>  
        public event EventHandler<EventArgs> Activated;

        /// <summary>  
        /// Method for raising the Activated event.  
        /// </summary>  
        protected internal virtual void OnActivated()
        {
            if (Activated != null)
                Activated(this, EventArgs.Empty);
        }

        public MenuItem(string caption)
        {
            Caption = caption;
        }

        public virtual void Initialize()
        {
        }

        public virtual void LoadContent()
        {
            if (Font == null)
                Font = XEngine.Instance.Content.Load<SpriteFont>("Fonts/menufont");
        }

        public virtual void UnloadContent()
        {
        }

        /// <summary>  
        /// Updates the menu entry.  
        /// </summary>  
        public virtual void Update(MenuScreen screen, bool isSelected)
        {
            // When the menu selection changes, entries gradually fade between  
            // their selected and deselected appearance, rather than instantly  
            // popping to the new state.  
            float fadeSpeed = (float) XEngine.Instance.GameTime.ElapsedGameTime.TotalSeconds*4;

            if (isSelected)
                selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
            else
                selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }

        /// <summary>  
        /// Draws the menu entry. This can be overridden to customize the appearance.  
        /// </summary>  
        public virtual void Draw(MenuScreen screen, Vector2 position, bool isSelected)
        {
            // Draw the selected entry in yellow, otherwise white.  
            Color color = isSelected ? Color.Yellow : Color.White;

            // Pulsate the size of the selected menu entry.  
            double time = XEngine.Instance.GameTime.TotalGameTime.TotalSeconds;

            float pulsate = (float) Math.Sin(time*6) + 1;

            float scale = 1 + pulsate*0.05f*selectionFade;

            // Draw text, centered on the middle of each line.
            Vector2 origin = new Vector2(0, Font.LineSpacing/2);

            XEngine.Instance.SpriteBatch.DrawString(Font, Caption, position, color, 0,
                                                     origin, scale, SpriteEffects.None, 0);
        }

        /// <summary>  
        /// Queries how much space this menu entry requires.  
        /// </summary>  
        public virtual int GetHeight(MenuScreen screen)
        {
            return Font.LineSpacing;
        }
    }
}